<?php

class UploadController extends My_Controller_Action
{

    public function indexAction()
    {
        $this->_redirect('/');
    }
    
    public function imageAction() {
        $key = $this->_getParam('key');

        if (!empty($_FILES)) {
            $uploadDir = My_Utils::buildTempPath(null, 'image');
            $tempFile = $_FILES[$key]['tmp_name'];
            $targetPath = rtrim($uploadDir, '/');
            // Validate the file type
            $fileTypes = array('jpg', 'jpeg', 'png'); // File extensions
            $fileParts = pathinfo($_FILES[$key]['name']);
            $fileName = uniqid();
            $ext = strtolower($fileParts['extension']);
            $targetFile = rtrim($targetPath, '/') . '/' . $fileName . '.' . $ext;
            if (in_array($ext, $fileTypes)) {
                move_uploaded_file($tempFile, $targetFile);
                if ($ext != 'jpg') {
                    $imgUtils = new My_ImageUtils();
                    $imgUtils->load($targetFile);
                    $targetJpgFile = rtrim($targetPath, '/') . '/' . $fileName . '.jpg';
                    $imgUtils->save($targetJpgFile);
                    @unlink($targetFile);
                    $targetFile = $targetJpgFile;
                }
                $fileName = $_FILES[$key]['name'];
                
                $daoImage = new Application_Model_DAO_Image();
                $image = $daoImage->addNew($targetFile, array(
                    'user_id' => $this->userId,
                    'filename' => $fileName,
                ));
                $code = $image['code'];
                $result = array(
                    'name' => $fileName,
                    'id' => $code,
                );
                
                $this->ajaxPacket = $result;
                $this->addSuccess($this->translate('XXX_IMAGE_UPLOAD_SUCCESS'));
                $this->_redirect($this->getReferer());
            } else {
                $this->addError($this->translate('XXX_IMAGE_UPLOAD_INCORRECT_TYPE'));
                $this->_redirect($this->getReferer());
            }
        } else {
            $this->addError($this->translate('XXX_IMAGE_UPLOAD_NOT_PHOTO_TO_UPLOAD'));
            $this->_redirect($this->getReferer());
        }
    }
    
    public function getReferer() {
        return $_SERVER['HTTP_REFERER'];
    }

    public function imageAction()
    {
        Zend_Layout::getMvcInstance()->disableLayout();
        $id = $this->_getParam('id', 0);
        $width = $this->_getParam('width');
        $height = $this->_getParam('height');
        $overScale = intval($this->_getParam('overScale', 0));
        $overScale = $overScale == 0 ? 0 : 1;
        $type = $this->_getParam('type');
        $file = implode('_', array($width, $height, $overScale)) . '.jpg';
        $cachePath = APPLICATION_PUBLIC . '/images/' . $id;
        if (!file_exists($cachePath)) {
            mkdir($cachePath, 0777, true);
        }
        $cachePath .= '/' . $file;
        if (file_exists($cachePath)) {
            $content = file_get_contents($cachePath);
            header('Content-Type: image/jpg');
            die($content);
        }
        $path = realpath(APPLICATION_PATH . '/../uploaded/image/') . '/' . $id . '.jpg';
        if (!file_exists($path)) {
            $path = realpath(APPLICATION_PATH . '/../uploaded/image/') . '/0.jpg';
        }
        $imgUtils = new My_ImageUtils();
        $imgUtils->load($path);
        $imgUtils->resizeToBounds($width, $height, $overScale);
        $imgUtils->save($cachePath);
        $content = file_get_contents($cachePath);
        header('Content-Type: image/jpg');
        die($content);
    }

}

